package net.lesscoding.message;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import net.lesscoding.rpc.message.RpcRequestMessage;
import net.lesscoding.rpc.message.RpcResponseMessage;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author eleven
 * @date 2024/10/31 8:33
 * @apiNote
 */
@Data
public abstract class Message implements Serializable {
    public static Class<?> getMessageClass(int messageType) {
        if (CollUtil.isEmpty(messageClasses)) {
            Collections.addAll(messageClasses,
                    Message.class, LoginRequestMessage.class, LoginResponseMessage.class, ChatRequestMessage.class, ChatResponseMessage.class,
                    GroupCreateRequestMessage.class, GroupCreateResponseMessage.class, GroupJoinRequestMessage.class, GroupJoinResponseMessage.class,
                    GroupQuitRequestMessage.class, GroupQuitResponseMessage.class, GroupChatRequestMessage.class, GroupChatResponseMessage.class,
                    GroupMembersRequestMessage.class, GroupMembersResponseMessage.class, HeartBeatMessage.class,
                    RpcRequestMessage.class, RpcResponseMessage.class
            );
        }
        return messageClasses.get(messageType);
    }

    private int sequenceId;

    private int messageType;

    public abstract int getMessageType();

    public static final int RPC_MESSAGE_TYPE_REQUEST = 16;
    public static final int RPC_MESSAGE_TYPE_RESPONSE = 17;

    public static final int DEFAULT_MSG = 0;
    public static final int LOGIN_REQUEST = 1;
    public static final int LOGIN_RESPONSE = 2;
    public static final int CHAT_REQUEST = 3;
    public static final int CHAT_RESPONSE = 4;
    public static final int GROUP_CREATE_REQUEST = 5;
    public static final int GROUP_CREATE_RESPONSE = 6;
    public static final int GROUP_JOIN_REQUEST = 7;
    public static final int GROUP_JOIN_RESPONSE = 8;
    public static final int GROUP_QUIT_REQUEST = 9;
    public static final int GROUP_QUIT_RESPONSE = 10;
    public static final int GROUP_CHAT_REQUEST = 11;
    public static final int GROUP_CHAT_RESPONSE = 12;
    public static final int GROUP_MEMBERS_REQUEST = 13;
    public static final int GROUP_MEMBERS_RESPONSE = 14;
    public static final int HEART_BEAT = 15;
    private static final List<Class> messageClasses = new ArrayList<>();
}
